var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
        isfree:0,
        selectedDate:'',
        datalist: [],
        pagenum: 1,
        cateArr:[],
        cateName:'',
        pics:[],
	    	cindex:0,
        isagree:true,
        id:0,
        selectIds:[],
        showleixing:false,
        gc:'',
        ss:'',
        ye:'',
        dhsj:'',
        diaohuotime:'',//日期
        cid:'',
        // idd:0,
  },
  
	_onLoad:function(e){
    // console.log('mypublic',this.data.info)
    console.log('opt111',this.options,this.options.id)
        var that = this;
        var id = 0;
        if(that.options.id){
            id = that.options.id
            console.log('idd传进来了存在444',id)
            // that.setData({idd:id});
            // console.log('idd333',that.data.idd)
        }
        console.log('idd没进来444',id)
       
		$.get('yuxun/fatiediaodian',{id:id},function(res){
      console.log('钓点数据111',res)
			if(res.status == 2){
				$.alert(res.msg,function(){
					if(res.tourl){
						$.goto(res.tourl,'redirect');
					}
				});
				return ;
			}
			var clist = res.clist
			var cateArr = [];
			for(var i in clist){
				cateArr.push(clist[i].name)
			}
			var pics = res.info ? res.info.pics : '';
			if(pics){
				pics = pics.split(',');
			}else{
				pics = [];
			}
			
			res.cateArr = cateArr;
			res.pics = pics;
	
			console.log("res000",res)
      that.setData(res)
      that.setData({cid:res.info.cid})
      console.log('clist111',that.data.clist)
      var cateName = ''
      that.data.clist.forEach((item,index)=>{
        if(cateName == '' && item.select){
          cateName = cateName + item.name
        }else if(cateName !='' && item.select){
          cateName =cateName + "," +item.name
        }
      })
     
      that.setData({cateName:cateName})
      console.log('333name',that.data.cateName)
    });
    that.getdiaodian();
    console.log('clist222',that.data.clist)

    },
   
    showleixing:function(){
        this.setData({showleixing:true})
    },
    getdiaodian(){
      let that = this
      console.log('521',)
      $.post('yuxun/getDataDetail',{id:that.data.id},res=>{
        console.log('res2232',res)
      })
     },
    hideleixing:function(){
        this.setData({showleixing:false})
    },
    changeTagType2:function(e){
        var  id = e.currentTarget.dataset.id;
        var selectIds = this.data.selectIds;
        var idx = e.currentTarget.dataset.index;
        let clist = [...this.data.clist];
        let obj = {};
        clist.forEach((item, index) => {
            if (idx === index) {
                obj.id = item.id;
                obj.name = item.name;
                obj.select = !item.select;
                clist.splice(index, 1, obj);
                obj = {};
            }
        });
        var selectIds = [];
        for(var i=0; i<clist.length;i++){
            if(clist[i].select==true){
                selectIds.push(clist[i].id)
            }
        }
        console.log('分类id111',selectIds)
        
        this.setData({clist:clist,selectIds:selectIds})
    },
    freeChange:function(e){
        var isfree = e.detail.value;
        console.log(isfree)
		this.setData({isfree:isfree})
	},
	cateChange:function(e){
		this.setData({cindex:e.detail.value})
    },
    handleDateChange: function (event) {
        const selectedDate = event.detail.value;
        console.log(selectedDate)
        this.setData({
            selectedDate: selectedDate,
        });
    },
	locationSelect:function(){
		var that = this
		wx.chooseLocation({success:function(res){
			console.log(res)
			that.setData({
				'info.address':res.name,
				'info.latitude':res.latitude,
				'info.longitude':res.longitude
			})
		}})
	},
	subform:function(e){
		var that = this;
        var info = e.detail.value;
        var selectIds = this.data.selectIds;
		if(info.name==''){
			$.error('请输入钓点名称');return false;
		}
		if(info.latitude==''){
			$.error('请选择坐标');return false;
		}
		if(info.address==''){
			$.error('请输入详细地址');return false;
        } 
        if(selectIds == ""){
            info.cid = that.data.cid
            console.log('selectids001',info.cid)
        }else{
          info.cid = selectIds.join(",");
        }

		if(info.pics==''){
			$.error('请上传钓点图片');return false;
		}
		/*
		if(that.data.selectedDate){
            info.diaohuotime = that.data.selectedDate
        }else{
            info.diaohuotime = "";
            $.error('请选择钓获时间');return false;
        }*/
		
		//info.cid = that.data.clist[that.data.cindex].id
		if(that.data.info && that.data.info.id){
			info.id = that.data.info.id;
        }

        info.isfree = that.data.isfree;
        
		// $.post("yuxun/fatiediaodian",{info:info,ganchang:'',shuishen:'',diaohuotime:'',},function(res){
      $.post("yuxun/fatiediaodian",{info:info,id:info.id},function(res){
      console.log('免费钓点发布',info)
			$.error(res.msg);
			setTimeout(function(){
                //$.goto($.indexurl);
                $.goback(true)
			},1000)
		},'提交中');
    },
    isagreeChange:function(e){
		console.log(e.detail.value)
		var val = e.detail.value
		if(val.length > 0){
			this.setData({isagree:true})
		}else{
			this.setData({isagree:false})
		}
	},
	showxieyi:function(){
		this.setData({showxieyi:true})
	},
	hidexieyi:function(){
		this.setData({showxieyi:false,isagree:true})
	},
	setname:function(e){
		this.setData({'info.name':e.detail.value})	
    },
    setprice:function(e){
		this.setData({'info.price':e.detail.value})	
    },
    setshuishen:function(e){
		this.setData({'info.shuishen':e.detail.value})	
    },
    setganchang:function(e){
		this.setData({'info.ganchang':e.detail.value})	
    },
    setyonger:function(e){
		this.setData({'info.yonger':e.detail.value})	
    },
    // 时间
    bindDateChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value,e)
      this.setData({
        diaohuotime: e.detail.value,
        
      })
      this.setfield(e)
    },
    setfield(e){
        var field = e.currentTarget.dataset.name;
        console.log('内容',field,e)
        if(field=="diaowei"){
            this.setData({'info.diaowei':e.detail.value})
        }
        if(field=="diyu"){
            this.setData({'info.diyu':e.detail.value})
        }
        if(field=="fangyu"){
            this.setData({'info.fangyu':e.detail.value})
        }
        if(field=="huiyu"){
            this.setData({'info.huiyu':e.detail.value})
        }
        if(field=='ganchang'){
          this.setData({'info.ganchang':e.detail.value})
          console.log('新内容',this.data.info)
        }
        if(field=='shuishen'){
          this.setData({'info.shuishen':e.detail.value})
          console.log('新内容')
        }
        if(field=='yonger'){
          this.setData({'info.yonger':e.detail.value})
          console.log('新内容',this.data.gc)
        }
        if(field=='diaohuotime'){
          this.setData({'info.diaohuotime':e.detail.value})
          console.log('新内容',e.detail.value)
        }
        
    },
}))